﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YhmOA.Common
{
    #region 服务端消息集合范性类
    /// <summary>
    /// 服务端消息集合范性类
    /// </summary>
    internal class ServerMessageList : List<ServerMessage>
    { }
    #endregion

    #region 服务端消息类
    /// <summary>
    /// 服务端消息类
    /// </summary>
    [Serializable]
    internal class ServerMessage
    {
        #region 消息的类型
        /// <summary>
        /// 消息的类型
        /// </summary>
        private ServerMessageType type;
        /// <summary>
        /// 消息的类型
        /// </summary>
        public ServerMessageType Type
        {
            get { return type; }
            set { type = value; }
        }
        #endregion

        #region 控件ID
        /// <summary>
        /// 控件ID
        /// </summary>
        private string id;
        /// <summary>
        /// 控件ID
        /// </summary>
        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        #endregion

        #region 消息内容
        /// <summary>
        /// 消息内容
        /// </summary>
        private string content;
        /// <summary>
        /// 消息内容
        /// </summary>
        public string Content
        {
            get { return content; }
            set { content = value; }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">消息类型</param>
        /// <param name="content">消息内容</param>
        public ServerMessage(ServerMessageType type, string content)
        {
            this.type = type;
            this.id = type.ToString().ToLower();
            this.content = content;
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">消息类型</param>
        /// <param name="id">控件ID</param>
        /// <param name="content">消息内容</param>
        public ServerMessage(ServerMessageType type,string id, string content)
        {
            this.type = type;
            this.id = id;
            this.content = content;
        }
        #endregion

        #region 过滤字符串
        /// <summary>
        /// 过滤字符串
        /// </summary>
        /// <param name="value">要过滤的字符串</param>
        /// <returns>返回过滤过的字符串</returns>
        private string FilterString(string value)
        {
            if (value == null)
            {
                return "";
            }
            return value.Replace(@"\", @"\\").Replace("\"", "\\\"").Replace("\r", @"\r").Replace("\n", @"\n");
        }
        #endregion

        #region 重写ToString()方法
        /// <summary>
        /// 重写ToString()方法
        /// </summary>
        /// <returns>返回格式化后的字符串</returns>
        public override string ToString()
        {
            return "{\"type\":\"" + FilterString(this.Type.ToString().ToLower()) + "\",\"id\":\"" + FilterString(this.Id) + "\",\"content\":\"" + FilterString(this.content) + "\"}";
        }
        #endregion
    }
    #endregion

    #region 服务端消息类型
    /// <summary>
    /// 服务端消息类型
    /// </summary>
    internal enum ServerMessageType
    { 
        /// <summary>
        /// 返回的是一个alert对话框
        /// </summary>
        Message,
        /// <summary>
        /// 返回的是一段要执行的脚本
        /// </summary>
        Script,
        /// <summary>
        /// 返回的是一个返回值
        /// </summary>
        Return,
        /// <summary>
        /// 返回的是一个控件
        /// </summary>
        Control,
        /// <summary>
        /// 返回视图状态
        /// </summary>
        ViewState
    }
    #endregion
}
